<?php
namespace app\admin\controller;
use app\model\ExamItem as ExamItemModel;
use app\model\Problem as ProblemModel;
use think\Db;


class ExamItem extends Base
{
    public function __construct(){
        $model = new ExamItemModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '焦点图';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $fields = [
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'image'=>['name'=>'图片','type'=>'file'],
            'status'=>['name'=>'状态','type'=>'select1','params'=>$model->getStatusList()],
            'sort'=>['name'=>'排序值','type'=>'text'],
            'description'=>['name'=>'描述','type'=>'textarea'],
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;

        $users = Db::name('user')->select();
        $users = arrayParamExtract($users,'id','nickname');
        $search_params = [
            [
                ['key'=>'ctime','name'=>'日期','type'=>'date'],
                ['key'=>'user_id','name'=>'用户','type'=>'select1_1','params'=>$users],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'ctime','type'=>'='],
            ['key'=>'user_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        // if ($exam_id > 0) {
        //     $exam = Db::name('exam')->where('id',$exam_id)->find();
        //     // $map[] = ['user_id','=',$exam['user_id']];
        //     // $map[] = ['ctime','=',$exam['ctime']];
        // }


        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->select();
        
        $subs = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        foreach ($list as $k => $vo) {
            $list[$k]['user'] = Db::name('user')->where('id',$vo['user_id'])->find();


            $problem = Db::name('problem')->where('id',$vo['problem_id'])->find();
            $problem['problem_bank_name'] = Db::name('problem_bank')->where('id',$problem['problem_bank_id'])->value('name');
            $problem['type_cn'] = ProblemModel::getTypeCn($problem['type']);

            $options = json_decode($problem['content'],true);
            foreach ($options as $option_k => $option_v) {
                $options[$option_k] = $subs[$option_k].'.'.$option_v;
            }
            $problem['content'] = implode('<br />', $options);
            $answers = explode(',', $problem['answer']);
            foreach ($answers as $answer_k => $answer_v) {
                $answers[$answer_k] = $subs[$answer_v];
            }
            $problem['answer'] = implode(',', $answers);

            $list[$k]['problem'] = $problem;

            $answers = explode(',', $vo['answer']);
            foreach ($answers as $answer_k => $answer_v) {
                $answers[$answer_k] = $subs[$answer_v];
            }
            $list[$k]['answer'] = implode(',', $answers);
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function export(){
        $model = $this->model;
        $map_params = [
            ['key'=>'ctime','type'=>'='],
            ['key'=>'problem_bank_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        if (isSearchParam('month')) {
            $map[] = ['ctime','like',input('month').'%'];
        }
        if (isSearchParam('years')) {
            $map[] = ['ctime','like',input('years').'%'];
        }

        if (input('problem_bank_id') > 0) {
            $list = Db::name('exam_item')->field('user_id,ctime')->where($map)->group('user_id,ctime')->select();
            foreach ($list as $k => $vo) {
                $list[$k]['total_snum'] = Db::name('exam_item')->where(array_merge($map,[
                    ['user_id','=',$vo['user_id']],
                    ['status','=',1]
                ]))->count();
                $list[$k]['total_enum'] = Db::name('exam_item')->where(array_merge($map,[
                    ['user_id','=',$vo['user_id']],
                    ['status','=',0]
                ]))->count();
            }
        }else{
            $list = $model->where($map)->order('id','desc')->select();
        }

        foreach ($list as $k => $vo) {
            $list[$k]['user'] = Db::name('user')->where('id',$vo['user_id'])->find();
            $total_num = $vo['total_snum'] + $vo['total_enum'];
            $list[$k]['total_num'] = $total_num;
            $list[$k]['success_radio'] = (round($vo['total_snum'] / $total_num,2)*100).'%';
        }
        $data = [];
        $title = ['日期','用户昵称','手机号','答题总数','正确数','错误数','正确率'];
        foreach ($list as $k => $vo) {
            $tmp = [
                $vo['ctime'],
                $vo['user']['nickname'],$vo['user']['mobile'],
                $vo['total_num'],$vo['total_snum'],$vo['total_enum'],
                $vo['success_radio']
            ];
            $data[] = $tmp;
        }
        exportCsv(date('YmdHis'),$title,$data);
    }

   
}
